/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 29489
 * Date: 2023-04-23
 * Time: 20:21
 */
import java.util.Scanner;
import java.util.Comparator;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        PersonSortable2 person[] = new PersonSortable2[n];
        for(int i = 0; i < n; i++){
            person[i] = new PersonSortable2(sc.next(), sc.nextInt());
        }
        java.util.Arrays.sort(person, new NameComparator());
        System.out.println("NameComparator:sort");
         for(PersonSortable2 e : person){
             System.out.println(e.toString());
         }
//        for(int i = 0; i < person.length; i++){
//            System.out.println(person[i].toString());
//        }
        //
        System.out.println("AgeComparator:sort");
        java.util.Arrays.sort(person, new AgeComparator());
        for(int i = 0; i < person.length; i++){
            System.out.println(person[i].toString());
        }
    }
}

class PersonSortable2{
    private String name;
    private int age;

    public PersonSortable2(String name, int age){
        this.name = name;
        this.age = age;
    }
    public String toString() {
        return name+"-"+age;
    }
    public String getName(){
        return name;
    }
    public int getAge(){
        return age;
    }
}
class NameComparator implements Comparator<PersonSortable2>{
    public int compare(PersonSortable2 per1, PersonSortable2 per2){
        if(per1.getName().compareTo(per2.getName()) > 1)
            return 1;
        else if(per1.getName().compareTo(per2.getName()) < 0)
            return -1;
        else
            return 0;
    }
}
class AgeComparator implements Comparator<PersonSortable2>{
    public int compare(PersonSortable2 per1, PersonSortable2 per2){
        if(per1.getAge() > per2.getAge())
            return 1;
        else if(per1.getAge() < per2.getAge())
            return -1;
        else
            return 0;
    }
}